#include "timertest.h"

TimerTest::TimerTest(QVBoxLayout *timeDisplayIn)
{
    started = false;
    timeDisplay = timeDisplayIn;

    // Heure courante du systeme
    currentTimeDisplay = new QTimeEdit(QTime::currentTime());
    timeDisplay->addWidget(currentTimeDisplay);
    currentTimeDisplay->setReadOnly(true);
    currentTimeDisplay->setButtonSymbols(QAbstractSpinBox::NoButtons);

    // Heure de lancement du test
    startTimeDisplay = new QTimeEdit();
    timeDisplay->addWidget(startTimeDisplay);
    startTimeDisplay->setReadOnly(true);
    startTimeDisplay->setButtonSymbols(QAbstractSpinBox::NoButtons);

    // Heure de fin estimee du test
    endTimeDisplay = new QTimeEdit;
    timeDisplay->addWidget(endTimeDisplay);
    endTimeDisplay->setReadOnly(true);
    endTimeDisplay->setButtonSymbols(QAbstractSpinBox::NoButtons);

    // Duree effective du test
    lengthTimeDisplay  = new QTimeEdit();
    timeDisplay->addWidget(lengthTimeDisplay);
    lengthTimeDisplay->setReadOnly(true);
    lengthTimeDisplay->setButtonSymbols(QAbstractSpinBox::NoButtons);

    // Lancement d'un timer Qt pour mettre a jour les temps en continu
    timer = new QTimer();
    timer->start(40);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
}

void TimerTest::start()
// Lancement du test
{
    started = true;
    timeStart = QTime::currentTime();
    startTimeDisplay->setTime(timeStart);
}

void TimerTest::stop()
// Arret du test
{
    started = false;
    startTimeDisplay->setTime(QTime(0,0));
    lengthTimeDisplay->setTime(QTime(0,0));
}

void TimerTest::refresh()
// Si un test est en cours, on affiche le temps de debut et la duree en plus de l'heure
{
    currentTimeDisplay->setTime(QTime::currentTime());
    if(started){
        // On met a jour la duree
        timeTest = QTime().addMSecs(timeStart.msecsTo(QTime::currentTime()));
        lengthTimeDisplay->setTime(timeTest);
    }
}
